<?php
declare (strict_types = 1);
/**
 * 素材模型
 */
namespace app\common\model;

class AdvPgmListDetail extends BaseModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 't_adv_pgmlist_detail';

    // 联查素材表
    public function matInfoHasOne()
    {
        return $this->hasOne(Material::class, 'mat_id', 'mat_id');
    }
    // 联查节目单日期表
    public function plHasOne()
    {
        return $this->hasOne(AdvPgmList::class, 'pl_id', 'pl_id');
    }
    // 联查节目时段表
    public function advseqHasOne()
    {
        return $this->hasOne(Advseq::class, 'adv_seq_id', 'advseg_id');
    }
    // 联查素材类型表表
    public function matTypeHasOne()
    {
        return $this->hasOne(MatType::class, 'mat_type_id', 'pgm_type');
    }

    public function detailLists($where = [], $ordername='play_order', $order= 'asc') {

        $whereMat = [];
        $wherepl  = [];
        $whereadv = [];
        $field = '*';

        $list = self::with([
            'matInfoHasOne' => function ($query) use ($whereMat) {
                $query->withField(['mat_id','mat_name', 'file_name', 'file_len', 'markin', 'markout'])->hidden(['mat_id'])->where($whereMat);
            },
            'plHasOne' => function ($query) use ($wherepl) {
                // 此处withField要写入主键，不然不会显示查询关联数据
                $query->withField(['pl_id','play_date'])->hidden(['pl_id'])->where($wherepl);
            },
            'advseqHasOne' => function ($query) use ($whereadv) {
                // 此处withField要写入主键，不然不会显示查询关联数据
                $query->withField(['adv_seq_id','adv_seg_name','s_time'])->hidden(['advseg_id'])->where($whereadv);
            },
            'matTypeHasOne' => function ($query) use ($wherepl) {
                // 此处withField要写入主键，不然不会显示查询关联数据
                $query->withField(['mat_type_id','mat_type_name'])->hidden(['pgm_type'])->where($wherepl);
            }
        ])->field($field)->where($where)->order([$ordername => $order])->select()->toArray();

        foreach($list as $k =>$v) {
            if($v['matInfoHasOne'] != null) {
                $list[$k] = array_merge($v['matInfoHasOne'], $list[$k]);                               
            }
            unset($list[$k]['matInfoHasOne']);

            if($v['plHasOne'] != null) {
                $list[$k] = array_merge($v['plHasOne'], $list[$k]);                               
            }
            unset($list[$k]['plHasOne']);

            if($v['matTypeHasOne'] != null) {
                $list[$k] = array_merge($v['matTypeHasOne'], $list[$k]);                               
            }
            unset($list[$k]['matTypeHasOne']);

            if($v['advseqHasOne'] != null) {
                $list[$k] = array_merge($v['advseqHasOne'], $list[$k]);                               
            }
            
            // 用于排序
            $s_time[$k]  = $list[$k]['advseqHasOne']['s_time'];
            $play_order[$k]  = $v['play_order'];
            // 广告库ID补0位
            $list[$k]['pgm_id']  = !empty($v['pgm_id']) ? sprintf("%08d", $v['pgm_id']) : null;
            // 素材ID补0位
            $list[$k]['mat_id']  = !empty($v['mat_id']) ? sprintf("%08d", $v['mat_id']) : null;

            unset($list[$k]['advseqHasOne']);
        }
        // 对最后的数组进行按照节目时段的开始时间正序排列
        if (!empty($s_time)) {
            array_multisort($s_time, SORT_ASC, $play_order, SORT_ASC, $list);
        }
 
        $s_time = '';
        $s_tatus = true;
        foreach ($list as $k => $val) {
            // 相同节目时段内容的素材详情增加相同的状态，不同节目时段交叉状态，以便于前端分组展示
            if (!empty($val['s_time'])) {
                if (empty($s_time)) {
                    // 第一次循环
                    $s_time = $val['s_time'];
                    $list[$k]['s_status'] = $s_tatus;
                }else{
                    if ($s_time == $val['s_time']) {
                        // 变量相等，则是同节目时段素材，s_status不变
                        $list[$k]['s_status'] = $s_tatus;
                    }elseif($s_time != $val['s_time']){
                        // 变量不相等，则是不同节目时段素材，s_status不变
                        $s_tatus = !$s_tatus;
                        $s_time = $val['s_time'];
                        $list[$k]['s_status'] = $s_tatus;
                    }
                }
            }
        }
        
        // $list = $this->getLastSql();     打印最后执行的一条SQL语句
        return $list;
    }

    // 查询最大排序值
    public function max_sort($param)
    {
        $count = self::where($param)->max('play_order');

        return $count;
    }

    // 统计素材条数
    public function max_count($param)
    {
        $count = self::where($param)->count('id');

        return $count;
    }

    // 统计日期单下面的时段分组
    public function groupadvseg($param)
    {
        $list = self::where($param)->group('advseg_id')->select()->toArray();

        return $list;
    }

    public function getadvseqLists($param)
    {
        
        $list = self::where($param)->select()->toArray();

        return $list;
    }
}
